package com.shilan.administrator.qintfm.fragment.tablayoutFragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;
import com.google.gson.Gson;
import com.shilan.administrator.qintfm.MainActivity;
import com.shilan.administrator.qintfm.R;
import com.shilan.administrator.qintfm.Utils.ViewPagerScrollerMaster;
import com.shilan.administrator.qintfm.adapter.RecommendAdapter;
import com.shilan.administrator.qintfm.adapter.RecommendViewPagerAdapter;
import com.shilan.administrator.qintfm.beans.RecommendResult;
import com.shilan.administrator.qintfm.volley.VolleySingleton;

import java.util.List;

import static android.view.MotionEvent.ACTION_MOVE;
import static android.view.MotionEvent.ACTION_UP;

/**
 * Created by dllo on 16/1/12.
 */
public class RecommendFragment extends Fragment {
    private RecyclerView recyclerView;
    private ViewPager recomViewPager;
    private RecommendAdapter recomAdapter;
    private RecommendViewPagerAdapter recViewAdapter;
    private ViewPagerScrollerMaster master;
   private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private RecyclerViewHeader rvHeader;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.fragment_recommend,null);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = (RecyclerView) view .findViewById(R.id.rv_recommend);
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),1));

        recomViewPager = (ViewPager) view.findViewById(R.id.viewPager_recommend);
        recViewAdapter = new RecommendViewPagerAdapter(getActivity().getSupportFragmentManager());

        rvHeader = (RecyclerViewHeader) view.findViewById(R.id.rvHeader_recommend);
        rvHeader.attachTo(recyclerView, true);

        master = new ViewPagerScrollerMaster();
        master.setRunnable(recomViewPager);
        master.startTurning(6000);

        recomViewPager.setAdapter(recViewAdapter);
        recomAdapter = new RecommendAdapter(getActivity());
        recyclerView.setAdapter(recomAdapter);
/**
 * 设置底部播放栏是否隐藏
 * **/
        recyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                    // 滑动时
                    case ACTION_MOVE:
                        MainActivity mainActivity = (MainActivity) getActivity();
                        // Visibility() MainActivity 中隐藏方法
                        mainActivity.Gone();
                        break;
                    // 手指移开时
                    case ACTION_UP:
                        MainActivity mActivity = (MainActivity) getActivity();
                         // MainActivity 中再次显示方法
                        mActivity.Visible();
                        break;
                }
                return false;
            }
        });

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    private void initData() {
        String url = "http://mobile.ximalaya.com/mobile/discovery/v1/recommends?channel=and-c6&device=android&includeActivity=true&includeSpecial=true&scale=2&version=4.3.44.2";

        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        RecommendResult recR = gson.fromJson(response,
                                RecommendResult.class);
                        if (recR != null) {
                            // 获得focusList集合并通过addData方法传给rvAdapter
                            RecommendResult.FocusImagesEntity focusImagesEntity =
                                    recR.getFocusImages();
                            if (focusImagesEntity != null) {
                                List<RecommendResult.FocusImagesEntity.ListEntity> focusList =
                                        focusImagesEntity.getList();
                                if (focusList != null && focusList.size() > 0) {
                                    recViewAdapter.addData(focusList);
                                }
                            }
                        }
                        RecommendResult.HotRecommendsEntity hot = recR.getHotRecommends();
                        if (hot != null) {
                            recomAdapter.addHotData(hot);
                        }

                    }
                },
                new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        VolleySingleton.getInstance().addRequest(request);
    }
}


